local cms_model = require("models.dao.cms")
local utils = require("library.utils")
local cjosn = require("cjson")
local uploader = require("library.uploader")
--分页模块
local Pagination = require("library.pagination")

--导航栏
local Nav = require("library.nav")
---------------------------------------------------
local IndexController = {}

--管理后台首页
function IndexController:index()
--    local session = require "resty.session".open({secret = "623q4hR325t36VsCD3g567922IC0073T"})
--    local username = session.data.username
--    if not username then
--        --登录失效跳转到的登陆页，温馨提示，如果使用ngx.redirect 必须在页面输出之前，比如ngx.say() ngx.pirnt 等。否则会报错
--        return ngx.redirect("login")
--    end

    local req = self:getRequest()

    --处理导航数据
    local nav_list = Nav(req.controller_name,req.action_name)
    local view = self:getView()
    local p = {
        username = username,
        nav_list = nav_list
    }
    local template_name = "admin/index.html"
    return view:render(template_name, p)
end

--后台登陆页
function IndexController:login()
    local view = self:getView()
    local p = {}
    local template_name = "admin/login.html"
    return view:render(template_name, p)
end

----------------------------
--  登陆提交
-- 设置session，请注意，必须要打开 lua_code_cache on，生产模式,
-- 如果关闭 lua_code_cache off ，必须要传入一个盐
----------------------------
function IndexController:login_sub()
    --得到请求的参数
    local req = self:getRequest()
    local username = req:getParam('username')
    local userpass = req:getParam('password')

    -- 判断用户名密码是否正确，贴别注意，lua and 跟 其他语言不一样
    if username ~= 'mind'  then
        ngx.say('用户名或密码错误')
        return ngx.exit(200)
    end

    if userpass ~= '123456'  then
        ngx.say('用户名或密码错误')
        return ngx.exit(200)
    end
    --开始设置session
    local session = require "resty.session".start({secret = "623q4hR325t36VsCD3g567922IC0073T"})
    session.data.username = username
    session:save()
    --跳转到后台首页
    return ngx.redirect("index")

end

----注销登录
function IndexController:logout()
    local session = require "resty.session".start({secret = "623q4hR325t36VsCD3g567922IC0073T"})
    session:destroy()
    ngx.redirect("login")
    return ''
end


------------------栏目模块开始--------------------------
--栏目管理
function IndexController:cate_list()
    local req = self:getRequest()
    local page = req:getParam('page') or 1
    local rows = req:getParam('rows') or 10

    --转成数字
    page = tonumber(page)
    rows = tonumber(rows)

    local cate_result = cms_model:cate_list(page,rows)
    --总条数
    local cate_total = cms_model:cate_list_total()
    --处理分页
    --总条数
    local count = tonumber(cate_total[1].total)
    local page_result =  Pagination:new({currentPage = page,totalItems = count,itemsPerPage = rows,action = req.uri,params = req.params})
    local page_list = page_result:getPage()
    --处理分页

    --处理导航数据
    local nav_list = Nav(req.controller_name,req.action_name)

    local view = self:getView()
    local p = {
        list = cate_result,
        page_list = page_list,
        nav_list = nav_list
    }
    --使用自定义模板，路径为views的路径
    local template_name = "admin/catelist.html"

    return view:render(template_name, p)
end

--栏目添加
function IndexController:cate_add()
    local req = self:getRequest()
    --处理导航数据
    local nav_list = Nav(req.controller_name,req.action_name)
    local view = self:getView()
    local p = {
        nav_list = nav_list
    }
    --使用自定义模板，路径为views的路径
    local template_name = "admin/cateadd.html"
    return view:render(template_name, p)
end

--栏目添加提交
function IndexController:cate_submit()
    --得到提交表单参数
    local req = self:getRequest()
    --栏目名称
    local cate_name = req:getParam("cate_name")

    --栏目路径
    local cate_path = req:getParam("cate_path")
    --栏目模板
    local cate_template = req:getParam("cate_template")

    --关键词
    local cate_keywords = req:getParam("cate_keywords")
    --描述
    local cate_desc = req:getParam("cate_desc")
    --是否启用seo标题
    local cate_seo = req:getParam("cate_seo")
    --转成整数类型
    cate_seo = tonumber(cate_seo)
    --seo标题
    local cate_seotitle = req:getParam("cate_seotitle")

    --权重值
    local cate_weight = req:getParam("cate_weight")
    --转成整数类型
    cate_weight = tonumber(cate_weight)
    --这里要做输入判断

    --这里要做输入判断
    local param = {
        cate_name = cate_name,
        cate_path = cate_path,
        cate_seotitle = cate_seotitle,
        cate_seo = cate_seo,
        cate_keywords = cate_keywords,
        cate_desc = cate_desc,
        cate_template = cate_template,
        cate_weight = cate_weight
    }

    --操作数据库
    local cate_add_result = cms_model:cate_add(param)
    --返回json
    return cjosn.encode(cate_add_result)
end


--栏目修改
function IndexController:cate_edit()

    local req = self:getRequest()
    --栏目id
    local cate_id = req:getParam("id")

    -- 得到栏目信息
    local cate_infos_result = cms_model:cate_infos(cate_id)

    --处理导航数据
    local nav_list = Nav(req.controller_name,req.action_name)

    local view = self:getView()
    local p = {
        cate = cate_infos_result[1],
        nav_list = nav_list
    }
    local template_name = "admin/cateedit.html"
    return view:render(template_name, p)
end

--栏目修改提交
function IndexController:cate_edit_submit()
    --得到提交表单参数
    local req = self:getRequest()
    --栏目id
    local cate_id = req:getParam("cate_id")
    cate_id = tonumber(cate_id)
    --栏目名称
    local cate_name = req:getParam("cate_name")

    --栏目路径
    local cate_path = req:getParam("cate_path")
    --栏目模板
    local cate_template = req:getParam("cate_template")

    --关键词
    local cate_keywords = req:getParam("cate_keywords")
    --描述
    local cate_desc = req:getParam("cate_desc")
    --是否启用seo标题
    local cate_seo = req:getParam("cate_seo")
    --转成整数类型
    cate_seo = tonumber(cate_seo)
    --seo标题
    local cate_seotitle = req:getParam("cate_seotitle")

    --权重值
    local cate_weight = req:getParam("cate_weight")
    --转成整数类型
    cate_weight = tonumber(cate_weight)
    --这里要做输入判断，以后有时间在做

    --这里要做输入判断，以后有时间在做
    local param = {
        cate_name,
        cate_path,
        cate_seotitle,
        cate_seo,
        cate_keywords,
        cate_desc,
        cate_template,
        cate_weight,
        cate_id
    }

    --操作数据库
    local cate_edit_result = cms_model:cate_update(param)
    --返回json
    return cjosn.encode(cate_edit_result)

end

-----------------------栏目模块结束---------------------------





-----------------------文章模块开始----------------------------

--文章列表管理
function IndexController:post_list()
    local req = self:getRequest()
    local page = req:getParam('page') or 1
    local rows = req:getParam('rows') or 10
    local status = req:getParam('status') or 1
    local cateId =  req:getParam('cate') or 0
    --转成数字
    page = tonumber(page) or 1
    rows = tonumber(rows) or 10
    status = tonumber(status) or 1
    cateId = tonumber(cateId) or 0
    --当前分页的结果集
    local posts_result = cms_model:posts_list(page,rows,status,cateId)
    --总条数
    local posts_total = cms_model:posts_list_total(status,cateId)

    --处理分页
    --总条数
    local count = tonumber(posts_total[1].total)
    local page_result =  Pagination:new({currentPage = page,totalItems = count,itemsPerPage = rows,action = req.uri,params = req.params})
    local page_list = page_result:getPage()
    --处理分页

    --处理导航数据
    local nav_list = Nav(req.controller_name,req.action_name)

    local view = self:getView()
    local p = {
        status = status,
        list = posts_result,
        empty = utils:is_table_empty(posts_result),
        page_list = page_list,
        nav_list = nav_list
    }
    local template_name = "admin/post_list.html"
    return view:render(template_name, p)
end


--文章添加
function IndexController:post_add()
    local req = self:getRequest()
    local cate_result = cms_model:cate_list(1,10000)
    --处理导航数据
    local nav_list = Nav(req.controller_name,req.action_name)
    local view = self:getView()
    local p = {
        list = cate_result,
        nav_list = nav_list
    }
    local template_name = "admin/post_add.html"
    return view:render(template_name, p)
end


--文章添加提交
function IndexController:post_submit()
    local req = self:getRequest()
    local add_param = req.params
    --转成数字类型
    add_param['cateid'] = tonumber(add_param.cateid)
    add_param['weight'] = tonumber(add_param['weight'])
    add_param['pubtime'] = math.floor(ngx.now())
    add_param['uptime'] = math.floor(ngx.now())
    add_param['flag'] =tonumber(add_param['flag'])
    add_param['click'] = tonumber(add_param['click'])
    local post_add_result = cms_model:posts_add(add_param)
    return cjosn.encode(post_add_result)
end


--文章修改
function IndexController:post_edit()
    local req = self:getRequest()
    local post_id = req:getParam('id')
    post_id = tonumber(post_id)

    local posts_infos_result = cms_model:posts_infos(post_id)

    local cate_result = cms_model:cate_list(1,10000)
    --处理导航数据
    local nav_list = Nav(req.controller_name,req.action_name)
    local view = self:getView()
    local p = {
        list = cate_result,
        posts = posts_infos_result[1],
        nav_list = nav_list
    }
    local template_name = "admin/post_edit.html"
    return view:render(template_name, p)
end

--修改文章提交
function IndexController:post_edit_submit()
    local req = self:getRequest()
    local edit_param = req.params
    --转成数字类型
    edit_param['id'] = tonumber(edit_param.id)
    edit_param['cateid'] = tonumber(edit_param.cateid)
    edit_param['weight'] = tonumber(edit_param['weight'])
    edit_param['flag'] = tonumber(edit_param['flag'])
    --操作数据库
    local posts_edit_result = cms_model:posts_update(edit_param)

    return cjosn.encode(posts_edit_result)
end

--删除文章
function IndexController:posts_status()
    local req = self:getRequest()
    local post_id = req:getParam('id')
    local status = req:getParam('status')
    post_id = tonumber(post_id)
    status = tonumber(status)
    --操作数据库
    local posts_staus_result = cms_model:posts_set_status(status,post_id)
    return cjosn.encode(posts_staus_result)
end


function IndexController:upload()
    --上传文件，
    local success, msg, origin_filename, extname, path, filename = uploader({dir = '/home/webserver/blog/www/uploads/litpic'})
    local result = {
        success = success,
        msg = '上传成功',
        extname = extname,
        origin_filename = origin_filename,
        path = '/uploads/litpic/'..filename
    }
    if success then
        return cjosn.encode(result)
    else
        local rs = {
            success = false,
            msg = '上传失败，请重试'
        }
        return cjosn.encode(rs)
    end

end

------------------------文章模块结束-----------------------------


-------------网站设置---------------------------------------
function IndexController:setting()
    local req = self:getRequest()
    local setting_result = cms_model:setting()
    --处理导航数据
    local nav_list = Nav(req.controller_name,req.action_name)
    local view = self:getView()
    local p = {
        setting = setting_result[1],
        nav_list = nav_list
    }
    local template_name = "admin/setting.html"
    return view:render(template_name, p)
end

function IndexController:setting_sub()
    local req = self:getRequest()
    local params = req.params
    local update_result = cms_model:websetting(params)
    return cjosn.encode(update_result)
end


-------------网站设置----------------------------------------


------------------修改密码------------------------
function IndexController:user()
    local req = self:getRequest()
    --处理导航数据
    local nav_list = Nav(req.controller_name,req.action_name)
    local view = self:getView()
    local p = {
        nav_list = nav_list
    }
    local template_name = "admin/user.html"
    return view:render(template_name, p)
end
------------------修改密码---------------------

return IndexController
